package com.zmn.plat.admin.controller.product.categorybrand;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.product.categorybrand.CategoryBrand;
import com.zmn.plat.services.interfaces.product.categorybrand.CategoryBrandService;

/**
 * 产品分类开通品牌表基础修改Controller
 *
 * @author tanghongjun
 * @since 2020-08-19 11:18:36
 */
@RestController
@RequestMapping("/product/categorybrand/")
public class CategoryBrandModifyController extends CategoryBrandBaseController {
    private Logger logger = LoggerFactory.getLogger(CategoryBrandListController.class);

    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 保存
     */
    @RequestMapping("addEdit")
    @RequiresPermissions("/product/categorybrand/addEditView.action")
    public ResponseDTO addEdit(@RequestBody List<CategoryBrand> categoryBrands) {
        if (CollectionUtils.isEmpty(categoryBrands)) {
            return ResponseDTO.fail("数据缺失！");
        }
        try {
            Integer result = categoryBrandService.insertBatch(categoryBrands, getMcStaffRealName());
            return ResponseDTO.success(result, "操作成功！");
        } catch (Exception e) {
            logger.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author linfeng
     * @since 2020-02-12 10:23:38
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/product/categorybrand/updateSort.action")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        Integer result = categoryBrandService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author linfeng
     * @since 2020-02-12 10:23:38
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/product/categorybrand/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        Integer result = categoryBrandService.updateStatusBatchByKey(arrayIds, status, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

}
